// pages/indexs/indexs.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    todoLists: [],
    value: '',
    obj: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const todoLists = (wx.getStorageSync('todoLists') && JSON.parse(wx.getStorageSync('todoLists'))) || []
    this.setData({
      todoLists: todoLists
    })
    wx.showToast({
      title: `还有${todoLists.length}个任务未完成`,
      icon: 'none'
    })
  },
  changeValue(e) {
    console.log(e)
    this.data.value = e.detail.value
  },
  addText() {
    this.data.todoLists.push({
      title: this.data.value,
      options: '删除'
    })
    this.setData({
      todoLists: this.data.todoLists,
      value: ''
    })
    wx.setStorageSync('todoLists', JSON.stringify(this.data.todoLists))
  },
  removeByIndex(e) {
    wx.showModal({
      title: '警告',
      content: '确定要删除吗？',
      success: (res) => {
        if (res.confirm) {
          this.data.todoLists.splice(e.target.id, 1)
          this.setData({
            todoLists: this.data.todoLists
          })
          wx.setStorageSync('todoLists', JSON.stringify(this.data.todoLists))
        } else if (res.cancel) {
          wx.showToast({
            title: '已取消删除',
            icon: 'none'
          })
        }
      }
    })

  }
})